package com.health.activity.settings;

import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.health.NovaActivity;
import com.health.NovaApplication;
import com.health.R;
import com.health.tools.GlobalConts;
import com.health.tools.SPUtil;

/**
 * 居民电子档案
 * 
 * @author nova
 *
 */
public class SystemSettingJMDZDAActivity extends NovaActivity implements View.OnClickListener {

	public Button save;// 保存
	public Button cancle;// 取消
	private static String pathStr = Environment.getExternalStorageDirectory() + GlobalConts.TYPE_DBFILENAME;
	private Button fanhui;
	private Button up;
	private TextView title;
	public EditText et_uploadaddress;// 上传地址
	private RadioGroup rg_isopen;// 是否开启
	private RadioButton rb_yes;// 开启
	private RadioButton rb_no;// 关闭

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_systemsetting_jmdzda);
		findViews();

		setListeners();
		initData();

	}

	public void initData() {

		boolean booleanDzjmjkk = SPUtil.getBOOLEAN_DZJMJKK(SystemSettingJMDZDAActivity.this);
		if (booleanDzjmjkk) {
			rb_yes.setChecked(true);
		} else {
			rb_yes.setChecked(false);
		}
		et_uploadaddress.setText(TextUtils.isEmpty(SPUtil.getSTR_DZJMJKK_ADDRESS(SystemSettingJMDZDAActivity.this)) ? ""
				: SPUtil.getSTR_DZJMJKK_ADDRESS(SystemSettingJMDZDAActivity.this));
	}

	@Override
	public void findViews() {
		save = (Button) findViewById(R.id.save);
		cancle = (Button) findViewById(R.id.cancle);
		fanhui = (Button) findViewById(R.id.btn_back);
		up = (Button) findViewById(R.id.btn_up);
		up.setVisibility(View.GONE);
		title = (TextView) findViewById(R.id.title);
		title.setText("设置电子居民健康卡");
		et_uploadaddress = (EditText) findViewById(R.id.et_uploadaddress);

		rg_isopen = (RadioGroup) findViewById(R.id.rg_isopen);
		rb_yes = (RadioButton) findViewById(R.id.rb_yes);
		rb_no = (RadioButton) findViewById(R.id.rb_no);
	}

	@Override
	public void setListeners() {

		save.setOnClickListener(this);
		cancle.setOnClickListener(this);
		fanhui.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();

			}
		});

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.save:
			// 保存设置
			saveSettings();
			break;
		case R.id.cancle:
			// 取消
			finish();
			break;

		case R.id.btn_back:
			// 返回
			finish();
			break;
		default:
			break;
		}
	}

	/**
	 * 保存数据
	 * 
	 * @author 王婷婷
	 * @date 2017年12月11日 下午4:35:26
	 */
	private void saveSettings() {

		if (rb_yes.isChecked()) {
			if (TextUtils.isEmpty(et_uploadaddress.getText().toString().trim())) {
				GlobalConts.showToast(SystemSettingJMDZDAActivity.this, "请输入上传地址");
				return;
			}
			SPUtil.setBOOLEAN_DZJMJKK(SystemSettingJMDZDAActivity.this, true);
			NovaApplication.setBoolean_dzjmjkk(true);
		} else if (rb_no.isChecked()) {
			SPUtil.setBOOLEAN_DZJMJKK(SystemSettingJMDZDAActivity.this, false);
			NovaApplication.setBoolean_dzjmjkk(false);

		}
		SPUtil.setSTR_DZJMJKK_ADDRESS(SystemSettingJMDZDAActivity.this,
				TextUtils.isEmpty(et_uploadaddress.getText().toString().trim()) ? ""
						: et_uploadaddress.getText().toString().trim());
		GlobalConts.showToast(SystemSettingJMDZDAActivity.this, "保存成功");

		finish();
	}
}
